<template>
  <div class="project-changes">
    <div style="margin-bottom: 10px">
      <el-button size="small" type="primary" @click="add">项目变更</el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="tableData"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="标题" prop="title" show-overflow-tooltip />
      <el-table-column
        label="变更类型"
        align="center"
        prop="changeTypeName"
        width="120"
      />
      <el-table-column
        label="变更原因"
        prop="changeReason"
        show-overflow-tooltip
      />
      <el-table-column
        label="变更前"
        prop="changeBefore"
        show-overflow-tooltip
      />
      <el-table-column
        label="变更后"
        prop="changeAfter"
        show-overflow-tooltip
      />
      <el-table-column
        label="申请人"
        align="center"
        prop="updateBy"
        width="120"
      />
      <el-table-column
        label="申请时间"
        align="center"
        prop="updateTime"
        width="170"
      />
      <el-table-column label="状态" align="center" width="120">
        <template slot-scope="{ row }">
          {{ renderStatus(row.status) }}
        </template>
      </el-table-column>
      <el-table-column label="审批结果" align="center" width="120">
        <template slot-scope="{ row }">
          {{ renderResultStatus(row.approveResult) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="160">
        <template slot-scope="{ row }">
          <el-button
            v-if="row.status == 4"
            size="mini"
            type="primary"
            plain
            @click="edit(row)"
          >
            编辑
          </el-button>
          <el-button
            v-else-if="row.status == 1"
            size="mini"
            type="danger"
            plain
            @click="revoke(row)"
          >
            撤销
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div style="margin-top: 10px" />
    <el-pagination
      layout="total, sizes, prev, pager, next, jumper"
      :page-sizes="[10, 20, 50, 100]"
      :current-page="pageNum"
      :page-size="pageSize"
      :total="total"
      @size-change="onPageSizeChange"
      @current-change="onCurrentPageChange"
    />

    <el-dialog
      width="640px"
      title="项目变更"
      :visible.sync="visible"
      :close-on-click-modal="false"
      :show-close="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="6em">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="变更类型" prop="changeType">
          <el-select v-model="form.changeType" placeholder="请选择变更类型">
            <el-option
              v-for="(item, index) in changeTypeList"
              :key="index"
              :value="item.key"
              :label="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="变更原因" prop="changeReason">
          <el-input
            v-model="form.changeReason"
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 5 }"
            placeholder="请输入变更原因"
          />
        </el-form-item>
        <el-form-item label="变更前" prop="changeBefore">
          <el-input
            v-model="form.changeBefore"
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 5 }"
            placeholder="请输入变更前"
          />
        </el-form-item>
        <el-form-item label="变更后" prop="changeAfter">
          <el-input
            v-model="form.changeAfter"
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 5 }"
            placeholder="请输入变更后"
          />
        </el-form-item>
        <el-form-item class="dialog-footer">
          <el-button @click="visible = false">关 闭</el-button>
          <el-button type="primary" @click="submit">提 交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  getChangePage,
  cancelChange,
  typeList,
  getDetails,
  submitProjectChange,
  updateProjectChange,
} from "@/api2/projectManagement/process";

export default {
  inject: ["projectId"],
  data() {
    return {
      tableData: [],
      listLoading: false,
      pageNum: 1,
      pageSize: 10,
      total: 0,
      visible: false,
      form: {},
      changeTypeList: [],
      rules: {
        title: [{ required: true, message: "标题不能为空", trigger: "blur" }],
        changeType: [
          { required: true, message: "变更类型不能为空", trigger: "change" },
        ],
        changeReason: [
          { required: true, message: "变更原因不能为空", trigger: "blur" },
        ],
        changeBefore: [
          { required: true, message: "变更前不能为空", trigger: "blur" },
        ],
        changeAfter: [
          { required: true, message: "变更后不能为空", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    this.getOption();
    this.getList();
  },
  methods: {
    renderStatus(status) {
      const obj = {
        1: "待审核",
        3: "已审核",
        4: "已撤销",
      };
      return obj[status];
    },
    renderResultStatus(status) {
      const obj = {
        0: "已通过",
        1: "已驳回",
      };
      return obj[status];
    },
    async getOption() {
      const { data } = await typeList();
      this.changeTypeList = data;
    },
    getList() {
      this.listLoading = true;
      getChangePage({
        projectId: this.projectId,
        pageNum: this.pageNum,
        pageSize: this.pageSize,
      }).then((data) => {
        this.listLoading = false;
        this.tableData = data.data;
        this.total = data.total;
      });
    },
    onPageSizeChange(val) {
      this.pageSize = val;
      this.pageNum = 1;
      this.getList();
    },
    onCurrentPageChange(val) {
      this.pageNum = val;
      this.getList();
    },
    add() {
      this.visible = true;
      this.resetForm();
      this.form = {};
    },
    async edit({ changeId }) {
      this.visible = true;
      const res = await getDetails({ changeId });
      this.form = res.data;
    },
    revoke({ changeId }) {
      this.$confirm("是否要撤销审批?", {
        type: "warning",
      })
        .then(async () => {
          await cancelChange({ changeId });
          this.$message.success("撤销成功");
          this.getList();
        })
        .catch(() => {});
    },
    submit() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          const {
            changeId,
            title,
            changeType,
            changeReason,
            changeAfter,
            changeBefore,
          } = this.form;
          const params = {
            projectId: this.projectId,
            title,
            changeType,
            changeReason,
            changeAfter,
            changeBefore,
          };
          if (changeId) {
            params.changeId = changeId;
            await updateProjectChange(params);
            this.$message.success("修改成功");
          } else {
            await submitProjectChange(params);
            this.$message.success("新增成功");
          }
          this.visible = false;
          this.getList();
        }
      });
    },
    resetForm() {
      if (!this.$refs.form) return;
      this.$refs.form.resetFields();
    },
  },
};
</script>

<style lang="scss" scoped>
.dialog-footer {
  margin-bottom: 0;
  text-align: center;
  ::v-deep {
    .el-form-item__content {
      margin-left: 0 !important;
    }
  }
}
</style>
